package com.lagou.transfer.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.spring.annotation.Autowired;
import com.spring.annotation.Component;
import com.spring.aop.DataSourceHandler;

import java.sql.Connection;
import java.sql.SQLException;

@Component
public class DruidDataSourceUtil implements DataSourceHandler {
    @Autowired
    private static DruidDataSource druidDataSource;
    // 使用这种方式实现不了，忘老师可以给出解答,谢谢
    private static ThreadLocal<Connection> connectionThreadLocal = new ThreadLocal<>();
    private static Connection connection;

    @Override
    public Connection getConnection() {
        try {
//            connection = connectionThreadLocal.get();
            if (connection == null) {
                connection = druidDataSource.getConnection();
                connectionThreadLocal.set(connection);
            }
            System.out.println(connection);
            return connection;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }
}
